package edu.rit.drh4818.raytracing.light;

import java.awt.Color;

import edu.rit.drh4818.raytracing.util.Intersection;
import edu.rit.drh4818.raytracing.util.Ray;
import edu.rit.drh4818.raytracing.util.Scene;

/**
 * 
 * A point light which has both specular and diffuse light color components.
 * 
 * @author Darren Headrick
 *
 */
public class PointLight
{
	/**
	 * The coordinates of the light.
	 */
	public final double x, y, z;
	
	/**
	 * Color of the light.
	 */
	public final Color color;
	
	/**
	 * 
	 * Create a new light source.
	 * 
	 * @param x X coordinate of light.
	 * @param y Y coordinate of light.
	 * @param z Z coordinate of light.
	 * @param color Color of the light.
	 */
	public PointLight( double x, double y, double z, Color color )
	{
		this.x = x;
		this.y = y;
		this.z = z;
		this.color = color;
	}
	
	/**
	 * Generate a ray to this light source, from the intersected point.
	 * 
	 * @param in Intersection to start from.
	 * @return Ray starting from intersection to light location.
	 */
	public Ray createShadowRay( Intersection in )
	{
		return Ray.rayFromPoints( in.getX(), in.getY(), in.getZ(), this.x, this.y, this.z );
	}

	public Color getColor(Intersection in) 
	{	
		Ray sray = createShadowRay( in );
		Color light = new Color( color.getRGB() );
		while( !sray.rayReachesPoint(x, y, z) )
		{
			if( !Scene.instance.softShadows ) return Color.black;
			in = sray.findNearestIntersection();
			if( in.obj.prop.kt == 0 ) return Color.BLACK;
			light = ColorMixer.multiply( light, in.obj.prop.kt );
			sray.moveTo( (int)(in.t + 1) );
		}
		
		return light;
	}
}
